package main.java;

/**
 * @author izumiqi
 * @date 2021/9/24 16:41:30
 * @des 个人实现的一个队列
 */
public class MyQueue {

    private Object[] queue;

    private int capacity;

    private int head;

    private int tail;

    private int size;

    public MyQueue(){
        queue = new Object[16];
        capacity = 16;
        head = tail = -1;
        size = 0;
    }

    public void setElement(Object o){
        if(tail == -1){
            queue[0] = o;
            head = 0;
            tail = 0;
            size++;
        }else{
            //判断是否需要移动头部指针
            if(size >= capacity){
                if(head + 1 > capacity -1){
                    head = head + 1 - capacity;
                }else{
                    head++;
                }
            }else {
                size++;
            }
            //判断是否需要形成循环队列
            if(tail + 1 > capacity -1){
                tail = tail + 1 - capacity;
                queue[tail] = o;
            }else{
                queue[++tail] = o;
            }
        }
    }

    public Object getElement(){
        if(head == -1){
            return null;
        }else{
            Object o = queue[head];
            if(head + 1 > capacity -1){
                head = head + 1 -capacity;
            }else{
                head++;
            }
            size--;
            return o;
        }
    }

    public int size(){
        return size;
    }
}
